<?php
//include APPLICATION_PATH."/models/base/ProvinceBase.php";
/* 
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
class Model_Report4{
    
    public function fetchAllReport($nam, $agent_ids=""){
        $db = Zend_Db_Table::getDefaultAdapter();
        //$select=$db->select()
               // ->from('province')
                //->//where('status=1');
        $andWhere = "";
        if(strlen($agent_ids)>0){
            $andWhere = " AND order.store_id IN (".$agent_ids.")";           
        }
        $stmt=$db->query("SELECT T.id, T.name_customer, 
                IFNULL(T1.quantity,0) as month1, IFNULL(T2.quantity,0) as month2, 
                IFNULL(T3.quantity,0) as month3, IFNULL(T4.quantity,0) as month4, 
                IFNULL(T5.quantity,0) as month5, IFNULL(T6.quantity,0) as month6, 
                IFNULL(T7.quantity,0) as month7, IFNULL(T8.quantity,0) as month8, 
                IFNULL(T9.quantity,0) as month9, IFNULL(T10.quantity,0) as month10, 
                IFNULL(T11.quantity,0) as month11, IFNULL(T12.quantity,0) as month12, 
                IFNULL(T1.quantity,0) + IFNULL(T2.quantity,0) + IFNULL(T3.quantity,0) + IFNULL(T4.quantity,0) + IFNULL(T5.quantity,0) + IFNULL(T6.quantity,0) + IFNULL(T7.quantity,0) + IFNULL(T8.quantity,0) + IFNULL(T9.quantity,0) + IFNULL(T10.quantity,0) + IFNULL(T11.quantity,0) + IFNULL(T12.quantity,0) as total_quantity,
                ROUND(IFNULL(T1.charges,0) + IFNULL(T2.charges,0) + IFNULL(T3.charges,0) + IFNULL(T4.charges,0) + IFNULL(T5.charges,0) + IFNULL(T6.charges,0) + IFNULL(T7.charges,0) + IFNULL(T8.charges,0) + IFNULL(T9.charges,0) + IFNULL(T10.charges,0) + IFNULL(T11.charges,0) + IFNULL(T12.charges,0),1) as charges
                FROM
                (
                SELECT DISTINCT agent.id, agent.name AS name_customer
                FROM  `order` 
                INNER JOIN  `order_detail` ON order.order_no = order_detail.order_id
                INNER JOIN  `agent` ON order.store_id = agent.id
                WHERE order_detail.order_status_id = 2 AND YEAR( order_detail.order_created ) =".$nam."
                    ".$andWhere." 
                ) AS T 
                LEFT JOIN
                (SELECT agent.id, agent.name AS name_customer, SUM( order_detail.quantity ) AS quantity, SUM( order_detail.charges ) AS charges
                FROM  `order` 
                INNER JOIN  `order_detail` ON order.order_no = order_detail.order_id
                INNER JOIN  `agent` ON order.store_id = agent.id
                WHERE order_detail.order_status_id = 2 and
                MONTH( order_detail.order_created ) =1
                AND YEAR( order_detail.order_created ) =".$nam."
                GROUP BY agent.id, agent.name) AS T1 ON T.id = T1.id
                LEFT JOIN 
                (SELECT agent.id, agent.name AS name_customer, SUM( order_detail.quantity ) AS quantity, SUM( order_detail.charges ) AS charges
                FROM  `order` 
                INNER JOIN  `order_detail` ON order.order_no = order_detail.order_id
                INNER JOIN  `agent` ON order.store_id = agent.id
                WHERE order_detail.order_status_id = 2 and
                MONTH( order_detail.order_created ) =2
                AND YEAR( order_detail.order_created ) =".$nam."
                GROUP BY agent.id, agent.name) AS T2 ON T.id = T2.id
                LEFT JOIN 
                (SELECT agent.id, agent.name AS name_customer, SUM( order_detail.quantity ) AS quantity, SUM( order_detail.charges ) AS charges
                FROM  `order` 
                INNER JOIN  `order_detail` ON order.order_no = order_detail.order_id
                INNER JOIN  `agent` ON order.store_id = agent.id
                WHERE order_detail.order_status_id = 2 and
                 MONTH( order_detail.order_created ) =3
                AND YEAR( order_detail.order_created ) =".$nam."
                GROUP BY agent.id, agent.name) AS T3 ON T.id = T3.id
                LEFT JOIN 
                (SELECT agent.id, agent.name AS name_customer, SUM( order_detail.quantity ) AS quantity, SUM( order_detail.charges ) AS charges
                FROM  `order` 
                INNER JOIN  `order_detail` ON order.order_no = order_detail.order_id
                INNER JOIN  `agent` ON order.store_id = agent.id
                WHERE order_detail.order_status_id = 2 and
                 MONTH( order_detail.order_created ) =4
                AND YEAR( order_detail.order_created ) =".$nam."
                GROUP BY agent.id, agent.name) AS T4 ON T.id = T4.id
                LEFT JOIN 
                (SELECT agent.id, agent.name AS name_customer, SUM( order_detail.quantity ) AS quantity, SUM( order_detail.charges ) AS charges
                FROM  `order` 
                INNER JOIN  `order_detail` ON order.order_no = order_detail.order_id
                INNER JOIN  `agent` ON order.store_id = agent.id
                WHERE order_detail.order_status_id = 2 and
                 MONTH( order_detail.order_created ) =5
                AND YEAR( order_detail.order_created ) =".$nam."
                GROUP BY agent.id, agent.name) AS T5 ON T.id = T5.id
                LEFT JOIN 
                (SELECT agent.id, agent.name AS name_customer, SUM( order_detail.quantity ) AS quantity, SUM( order_detail.charges ) AS charges
                FROM  `order` 
                INNER JOIN  `order_detail` ON order.order_no = order_detail.order_id
                INNER JOIN  `agent` ON order.store_id = agent.id
                WHERE order_detail.order_status_id = 2 and
                 MONTH( order_detail.order_created ) =6
                AND YEAR( order_detail.order_created ) =".$nam."
                GROUP BY agent.id, agent.name) AS T6 ON T.id = T6.id
                LEFT JOIN 
                (SELECT agent.id, agent.name AS name_customer, SUM( order_detail.quantity ) AS quantity, SUM( order_detail.charges ) AS charges
                FROM  `order` 
                INNER JOIN  `order_detail` ON order.order_no = order_detail.order_id
                INNER JOIN  `agent` ON order.store_id = agent.id
                WHERE order_detail.order_status_id = 2 and
                 MONTH( order_detail.order_created ) =7
                AND YEAR( order_detail.order_created ) =".$nam."
                GROUP BY agent.id, agent.name) AS T7 ON T.id = T7.id
                LEFT JOIN 
                (SELECT agent.id, agent.name AS name_customer, SUM( order_detail.quantity ) AS quantity, SUM( order_detail.charges ) AS charges
                FROM  `order` 
                INNER JOIN  `order_detail` ON order.order_no = order_detail.order_id
                INNER JOIN  `agent` ON order.store_id = agent.id
                WHERE order_detail.order_status_id = 2 and
                 MONTH( order_detail.order_created ) =8
                AND YEAR( order_detail.order_created ) =".$nam."
                GROUP BY agent.id, agent.name) AS T8 ON T.id = T8.id
                LEFT JOIN 
                (SELECT agent.id, agent.name AS name_customer, SUM( order_detail.quantity ) AS quantity, SUM( order_detail.charges ) AS charges
                FROM  `order` 
                INNER JOIN  `order_detail` ON order.order_no = order_detail.order_id
                INNER JOIN  `agent` ON order.store_id = agent.id
                WHERE order_detail.order_status_id = 2 and
                 MONTH( order_detail.order_created ) =9
                AND YEAR( order_detail.order_created ) =".$nam."
                GROUP BY agent.id, agent.name) AS T9 ON T.id = T9.id
                LEFT JOIN 
                (SELECT agent.id, agent.name AS name_customer, SUM( order_detail.quantity ) AS quantity, SUM( order_detail.charges ) AS charges
                FROM  `order` 
                INNER JOIN  `order_detail` ON order.order_no = order_detail.order_id
                INNER JOIN  `agent` ON order.store_id = agent.id
                WHERE order_detail.order_status_id = 2 and
                 MONTH( order_detail.order_created ) =10
                AND YEAR( order_detail.order_created ) =".$nam."
                GROUP BY agent.id, agent.name) AS T10 ON T.id = T10.id
                LEFT JOIN 
                (SELECT agent.id, agent.name AS name_customer, SUM( order_detail.quantity ) AS quantity, SUM( order_detail.charges ) AS charges
                FROM  `order` 
                INNER JOIN  `order_detail` ON order.order_no = order_detail.order_id
                INNER JOIN  `agent` ON order.store_id = agent.id
                WHERE order_detail.order_status_id = 2 and
                 MONTH( order_detail.order_created ) =11
                AND YEAR( order_detail.order_created ) =".$nam."
                GROUP BY agent.id, agent.name) AS T11 ON T.id = T11.id
                LEFT JOIN 
                (SELECT agent.id, agent.name AS name_customer, SUM( order_detail.quantity ) AS quantity, SUM( order_detail.charges ) AS charges
                FROM  `order` 
                INNER JOIN  `order_detail` ON order.order_no = order_detail.order_id
                INNER JOIN  `agent` ON order.store_id = agent.id
                WHERE order_detail.order_status_id = 2 and
                 MONTH( order_detail.order_created ) =12
                AND YEAR( order_detail.order_created ) =".$nam."
                GROUP BY agent.id, agent.name) AS T12 ON T.id = T12.id");
        $rows = $stmt->fetchAll(PDO::FETCH_CLASS);
        $stmt->closeCursor();
        $entries   = array();
        foreach ($rows as $row) {
            //$entry = new Model_Province();
            array_push($entries, $row);
            //$entries[] = $entry;
        }
        return $entries;
    }
    
     public function fetchAllReportExport($nam, $agent_ids=""){
        $db = Zend_Db_Table::getDefaultAdapter();
        $andWhere = "";
        if(strlen($agent_ids)>0){
            $andWhere = " AND order.store_id IN (".$agent_ids.")";           
        }
        //$select=$db->select()
               // ->from('province')
                //->//where('status=1');
        $stmt=$db->query("SELECT T.id, T.name_customer, 
                IFNULL(T1.quantity,0) as month1, IFNULL(T2.quantity,0) as month2, 
                IFNULL(T3.quantity,0) as month3, IFNULL(T4.quantity,0) as month4, 
                IFNULL(T5.quantity,0) as month5, IFNULL(T6.quantity,0) as month6, 
                IFNULL(T7.quantity,0) as month7, IFNULL(T8.quantity,0) as month8, 
                IFNULL(T9.quantity,0) as month9, IFNULL(T10.quantity,0) as month10, 
                IFNULL(T11.quantity,0) as month11, IFNULL(T12.quantity,0) as month12, 
                IFNULL(T1.quantity,0) + IFNULL(T2.quantity,0) + IFNULL(T3.quantity,0) + IFNULL(T4.quantity,0) + IFNULL(T5.quantity,0) + IFNULL(T6.quantity,0) + IFNULL(T7.quantity,0) + IFNULL(T8.quantity,0) + IFNULL(T9.quantity,0) + IFNULL(T10.quantity,0) + IFNULL(T11.quantity,0) + IFNULL(T12.quantity,0) as total_quantity,
                ROUND(IFNULL(T1.charges,0) + IFNULL(T2.charges,0) + IFNULL(T3.charges,0) + IFNULL(T4.charges,0) + IFNULL(T5.charges,0) + IFNULL(T6.charges,0) + IFNULL(T7.charges,0) + IFNULL(T8.charges,0) + IFNULL(T9.charges,0) + IFNULL(T10.charges,0) + IFNULL(T11.charges,0) + IFNULL(T12.charges,0),1) as charges
                FROM
                (
                SELECT DISTINCT agent.id, agent.name AS name_customer 
                FROM  `order` 
                INNER JOIN  `order_detail` ON order.order_no = order_detail.order_id
                INNER JOIN  `agent` ON order.store_id = agent.id
                WHERE order_detail.order_status_id = 2 and agent.agent_status !=3  AND YEAR( order_detail.order_created ) =".$nam."
                    ".$andWhere." 
                ) AS T 
                LEFT JOIN
                (SELECT agent.id, agent.name AS name_customer, SUM( order_detail.quantity ) AS quantity, SUM( order_detail.charges ) AS charges
                FROM  `order` 
                INNER JOIN  `order_detail` ON order.order_no = order_detail.order_id
                INNER JOIN  `agent` ON order.store_id = agent.id
                WHERE order_detail.order_status_id = 2 and
                MONTH( order_detail.order_created ) =1
                AND YEAR( order_detail.order_created ) =".$nam."
                GROUP BY agent.id, agent.name) AS T1 ON T.id = T1.id
                LEFT JOIN 
                (SELECT agent.id, agent.name AS name_customer, SUM( order_detail.quantity ) AS quantity, SUM( order_detail.charges ) AS charges
                FROM  `order` 
                INNER JOIN  `order_detail` ON order.order_no = order_detail.order_id
                INNER JOIN  `agent` ON order.store_id = agent.id
                WHERE order_detail.order_status_id = 2 and
                MONTH( order_detail.order_created ) =2
                AND YEAR( order_detail.order_created ) =".$nam."
                GROUP BY agent.id, agent.name) AS T2 ON T.id = T2.id
                LEFT JOIN 
                (SELECT agent.id, agent.name AS name_customer, SUM( order_detail.quantity ) AS quantity, SUM( order_detail.charges ) AS charges
                FROM  `order` 
                INNER JOIN  `order_detail` ON order.order_no = order_detail.order_id
                INNER JOIN  `agent` ON order.store_id = agent.id
                WHERE order_detail.order_status_id = 2 and
                 MONTH( order_detail.order_created ) =3
                AND YEAR( order_detail.order_created ) =".$nam."
                GROUP BY agent.id, agent.name) AS T3 ON T.id = T3.id
                LEFT JOIN 
                (SELECT agent.id, agent.name AS name_customer, SUM( order_detail.quantity ) AS quantity, SUM( order_detail.charges ) AS charges
                FROM  `order` 
                INNER JOIN  `order_detail` ON order.order_no = order_detail.order_id
                INNER JOIN  `agent` ON order.store_id = agent.id
                WHERE order_detail.order_status_id = 2 and
                 MONTH( order_detail.order_created ) =4
                AND YEAR( order_detail.order_created ) =".$nam."
                GROUP BY agent.id, agent.name) AS T4 ON T.id = T4.id
                LEFT JOIN 
                (SELECT agent.id, agent.name AS name_customer, SUM( order_detail.quantity ) AS quantity, SUM( order_detail.charges ) AS charges
                FROM  `order` 
                INNER JOIN  `order_detail` ON order.order_no = order_detail.order_id
                INNER JOIN  `agent` ON order.store_id = agent.id
                WHERE order_detail.order_status_id = 2 and
                 MONTH( order_detail.order_created ) =5
                AND YEAR( order_detail.order_created ) =".$nam."
                GROUP BY agent.id, agent.name) AS T5 ON T.id = T5.id
                LEFT JOIN 
                (SELECT agent.id, agent.name AS name_customer, SUM( order_detail.quantity ) AS quantity, SUM( order_detail.charges ) AS charges
                FROM  `order` 
                INNER JOIN  `order_detail` ON order.order_no = order_detail.order_id
                INNER JOIN  `agent` ON order.store_id = agent.id
                WHERE order_detail.order_status_id = 2 and
                 MONTH( order_detail.order_created ) =6
                AND YEAR( order_detail.order_created ) =".$nam."
                GROUP BY agent.id, agent.name) AS T6 ON T.id = T6.id
                LEFT JOIN 
                (SELECT agent.id, agent.name AS name_customer, SUM( order_detail.quantity ) AS quantity, SUM( order_detail.charges ) AS charges
                FROM  `order` 
                INNER JOIN  `order_detail` ON order.order_no = order_detail.order_id
                INNER JOIN  `agent` ON order.store_id = agent.id
                WHERE order_detail.order_status_id = 2 and
                 MONTH( order_detail.order_created ) =7
                AND YEAR( order_detail.order_created ) =".$nam."
                GROUP BY agent.id, agent.name) AS T7 ON T.id = T7.id
                LEFT JOIN 
                (SELECT agent.id, agent.name AS name_customer, SUM( order_detail.quantity ) AS quantity, SUM( order_detail.charges ) AS charges
                FROM  `order` 
                INNER JOIN  `order_detail` ON order.order_no = order_detail.order_id
                INNER JOIN  `agent` ON order.store_id = agent.id
                WHERE order_detail.order_status_id = 2 and
                 MONTH( order_detail.order_created ) =8
                AND YEAR( order_detail.order_created ) =".$nam."
                GROUP BY agent.id, agent.name) AS T8 ON T.id = T8.id
                LEFT JOIN 
                (SELECT agent.id, agent.name AS name_customer, SUM( order_detail.quantity ) AS quantity, SUM( order_detail.charges ) AS charges
                FROM  `order` 
                INNER JOIN  `order_detail` ON order.order_no = order_detail.order_id
                INNER JOIN  `agent` ON order.store_id = agent.id
                WHERE order_detail.order_status_id = 2 and
                 MONTH( order_detail.order_created ) =9
                AND YEAR( order_detail.order_created ) =".$nam."
                GROUP BY agent.id, agent.name) AS T9 ON T.id = T9.id
                LEFT JOIN 
                (SELECT agent.id, agent.name AS name_customer, SUM( order_detail.quantity ) AS quantity, SUM( order_detail.charges ) AS charges
                FROM  `order` 
                INNER JOIN  `order_detail` ON order.order_no = order_detail.order_id
                INNER JOIN  `agent` ON order.store_id = agent.id
                WHERE order_detail.order_status_id = 2 and
                 MONTH( order_detail.order_created ) =10
                AND YEAR( order_detail.order_created ) =".$nam."
                GROUP BY agent.id, agent.name) AS T10 ON T.id = T10.id
                LEFT JOIN 
                (SELECT agent.id, agent.name AS name_customer, SUM( order_detail.quantity ) AS quantity, SUM( order_detail.charges ) AS charges
                FROM  `order` 
                INNER JOIN  `order_detail` ON order.order_no = order_detail.order_id
                INNER JOIN  `agent` ON order.store_id = agent.id
                WHERE order_detail.order_status_id = 2 and
                 MONTH( order_detail.order_created ) =11
                AND YEAR( order_detail.order_created ) =".$nam."
                GROUP BY agent.id, agent.name) AS T11 ON T.id = T11.id
                LEFT JOIN 
                (SELECT agent.id, agent.name AS name_customer, SUM( order_detail.quantity ) AS quantity, SUM( order_detail.charges ) AS charges
                FROM  `order` 
                INNER JOIN  `order_detail` ON order.order_no = order_detail.order_id
                INNER JOIN  `agent` ON order.store_id = agent.id
                WHERE order_detail.order_status_id = 2 and
                 MONTH( order_detail.order_created ) =12
                AND YEAR( order_detail.order_created ) =".$nam."
                GROUP BY agent.id, agent.name) AS T12 ON T.id = T12.id
                
                ");
        $rows = $stmt->fetchAll(PDO::FETCH_CLASS);
        $stmt->closeCursor();
        $entries   = array();
        foreach ($rows as $row) {
            //$entry = new Model_Province();
            array_push($entries, $row);
            //$entries[] = $entry;
        }
        return $entries;
    }
    
    public function fetchAllReportExportByKinhDoanh($nam,$admin_id, $agent_ids){
        $db = Zend_Db_Table::getDefaultAdapter();
        $andWhere = "";
        if(strlen($agent_ids)>0){
            $andWhere = " AND order.store_id IN (".$agent_ids.")";           
        }
        //$select=$db->select()
               // ->from('province')
                //->//where('status=1');
        $stmt=$db->query("SELECT T.id, T.name_customer, 
                IFNULL(T1.quantity,0) as month1, IFNULL(T2.quantity,0) as month2, 
                IFNULL(T3.quantity,0) as month3, IFNULL(T4.quantity,0) as month4, 
                IFNULL(T5.quantity,0) as month5, IFNULL(T6.quantity,0) as month6, 
                IFNULL(T7.quantity,0) as month7, IFNULL(T8.quantity,0) as month8, 
                IFNULL(T9.quantity,0) as month9, IFNULL(T10.quantity,0) as month10, 
                IFNULL(T11.quantity,0) as month11, IFNULL(T12.quantity,0) as month12, 
                IFNULL(T1.quantity,0) + IFNULL(T2.quantity,0) + IFNULL(T3.quantity,0) + IFNULL(T4.quantity,0) + IFNULL(T5.quantity,0) + IFNULL(T6.quantity,0) + IFNULL(T7.quantity,0) + IFNULL(T8.quantity,0) + IFNULL(T9.quantity,0) + IFNULL(T10.quantity,0) + IFNULL(T11.quantity,0) + IFNULL(T12.quantity,0) as total_quantity,
                ROUND(IFNULL(T1.charges,0) + IFNULL(T2.charges,0) + IFNULL(T3.charges,0) + IFNULL(T4.charges,0) + IFNULL(T5.charges,0) + IFNULL(T6.charges,0) + IFNULL(T7.charges,0) + IFNULL(T8.charges,0) + IFNULL(T9.charges,0) + IFNULL(T10.charges,0) + IFNULL(T11.charges,0) + IFNULL(T12.charges,0),1) as charges
                FROM
                (
                SELECT DISTINCT agent.id, agent.name AS name_customer
                FROM  `order` 
                INNER JOIN  `order_detail` ON order.order_no = order_detail.order_id
                INNER JOIN  `agent` ON order.store_id = agent.id
                WHERE order_detail.order_status_id = 2 and agent.agent_status !=3 and agent.province_id in (Select admin_area.area_id from admin left join admin_area on admin.id= admin_area.admin_id where admin.id='".$admin_id."')
                    AND YEAR( order_detail.order_created ) =".$nam." ".$andWhere."
                ) AS T 
                LEFT JOIN
                (SELECT agent.id, agent.name AS name_customer, SUM( order_detail.quantity ) AS quantity, SUM( order_detail.charges ) AS charges
                FROM  `order` 
                INNER JOIN  `order_detail` ON order.order_no = order_detail.order_id
                INNER JOIN  `agent` ON order.store_id = agent.id
                WHERE order_detail.order_status_id = 2 and
                MONTH( order_detail.order_created ) =1
                AND YEAR( order_detail.order_created ) =".$nam."
                GROUP BY agent.id, agent.name) AS T1 ON T.id = T1.id
                LEFT JOIN 
                (SELECT agent.id, agent.name AS name_customer, SUM( order_detail.quantity ) AS quantity, SUM( order_detail.charges ) AS charges
                FROM  `order` 
                INNER JOIN  `order_detail` ON order.order_no = order_detail.order_id
                INNER JOIN  `agent` ON order.store_id = agent.id
                WHERE order_detail.order_status_id = 2 and
                MONTH( order_detail.order_created ) =2
                AND YEAR( order_detail.order_created ) =".$nam."
                GROUP BY agent.id, agent.name) AS T2 ON T.id = T2.id
                LEFT JOIN 
                (SELECT agent.id, agent.name AS name_customer, SUM( order_detail.quantity ) AS quantity, SUM( order_detail.charges ) AS charges
                FROM  `order` 
                INNER JOIN  `order_detail` ON order.order_no = order_detail.order_id
                INNER JOIN  `agent` ON order.store_id = agent.id
                WHERE order_detail.order_status_id = 2 and
                 MONTH( order_detail.order_created ) =3
                AND YEAR( order_detail.order_created ) =".$nam."
                GROUP BY agent.id, agent.name) AS T3 ON T.id = T3.id
                LEFT JOIN 
                (SELECT agent.id, agent.name AS name_customer, SUM( order_detail.quantity ) AS quantity, SUM( order_detail.charges ) AS charges
                FROM  `order` 
                INNER JOIN  `order_detail` ON order.order_no = order_detail.order_id
                INNER JOIN  `agent` ON order.store_id = agent.id
                WHERE order_detail.order_status_id = 2 and
                 MONTH( order_detail.order_created ) =4
                AND YEAR( order_detail.order_created ) =".$nam."
                GROUP BY agent.id, agent.name) AS T4 ON T.id = T4.id
                LEFT JOIN 
                (SELECT agent.id, agent.name AS name_customer, SUM( order_detail.quantity ) AS quantity, SUM( order_detail.charges ) AS charges
                FROM  `order` 
                INNER JOIN  `order_detail` ON order.order_no = order_detail.order_id
                INNER JOIN  `agent` ON order.store_id = agent.id
                WHERE order_detail.order_status_id = 2 and
                 MONTH( order_detail.order_created ) =5
                AND YEAR( order_detail.order_created ) =".$nam."
                GROUP BY agent.id, agent.name) AS T5 ON T.id = T5.id
                LEFT JOIN 
                (SELECT agent.id, agent.name AS name_customer, SUM( order_detail.quantity ) AS quantity, SUM( order_detail.charges ) AS charges
                FROM  `order` 
                INNER JOIN  `order_detail` ON order.order_no = order_detail.order_id
                INNER JOIN  `agent` ON order.store_id = agent.id
                WHERE order_detail.order_status_id = 2 and
                 MONTH( order_detail.order_created ) =6
                AND YEAR( order_detail.order_created ) =".$nam."
                GROUP BY agent.id, agent.name) AS T6 ON T.id = T6.id
                LEFT JOIN 
                (SELECT agent.id, agent.name AS name_customer, SUM( order_detail.quantity ) AS quantity, SUM( order_detail.charges ) AS charges
                FROM  `order` 
                INNER JOIN  `order_detail` ON order.order_no = order_detail.order_id
                INNER JOIN  `agent` ON order.store_id = agent.id
                WHERE order_detail.order_status_id = 2 and
                 MONTH( order_detail.order_created ) =7
                AND YEAR( order_detail.order_created ) =".$nam."
                GROUP BY agent.id, agent.name) AS T7 ON T.id = T7.id
                LEFT JOIN 
                (SELECT agent.id, agent.name AS name_customer, SUM( order_detail.quantity ) AS quantity, SUM( order_detail.charges ) AS charges
                FROM  `order` 
                INNER JOIN  `order_detail` ON order.order_no = order_detail.order_id
                INNER JOIN  `agent` ON order.store_id = agent.id
                WHERE order_detail.order_status_id = 2 and
                 MONTH( order_detail.order_created ) =8
                AND YEAR( order_detail.order_created ) =".$nam."
                GROUP BY agent.id, agent.name) AS T8 ON T.id = T8.id
                LEFT JOIN 
                (SELECT agent.id, agent.name AS name_customer, SUM( order_detail.quantity ) AS quantity, SUM( order_detail.charges ) AS charges
                FROM  `order` 
                INNER JOIN  `order_detail` ON order.order_no = order_detail.order_id
                INNER JOIN  `agent` ON order.store_id = agent.id
                WHERE order_detail.order_status_id = 2 and
                 MONTH( order_detail.order_created ) =9
                AND YEAR( order_detail.order_created ) =".$nam."
                GROUP BY agent.id, agent.name) AS T9 ON T.id = T9.id
                LEFT JOIN 
                (SELECT agent.id, agent.name AS name_customer, SUM( order_detail.quantity ) AS quantity, SUM( order_detail.charges ) AS charges
                FROM  `order` 
                INNER JOIN  `order_detail` ON order.order_no = order_detail.order_id
                INNER JOIN  `agent` ON order.store_id = agent.id
                WHERE order_detail.order_status_id = 2 and
                 MONTH( order_detail.order_created ) =10
                AND YEAR( order_detail.order_created ) =".$nam."
                GROUP BY agent.id, agent.name) AS T10 ON T.id = T10.id
                LEFT JOIN 
                (SELECT agent.id, agent.name AS name_customer, SUM( order_detail.quantity ) AS quantity, SUM( order_detail.charges ) AS charges
                FROM  `order` 
                INNER JOIN  `order_detail` ON order.order_no = order_detail.order_id
                INNER JOIN  `agent` ON order.store_id = agent.id
                WHERE order_detail.order_status_id = 2 and
                 MONTH( order_detail.order_created ) =11
                AND YEAR( order_detail.order_created ) =".$nam."
                GROUP BY agent.id, agent.name) AS T11 ON T.id = T11.id
                LEFT JOIN 
                (SELECT agent.id, agent.name AS name_customer, SUM( order_detail.quantity ) AS quantity, SUM( order_detail.charges ) AS charges
                FROM  `order` 
                INNER JOIN  `order_detail` ON order.order_no = order_detail.order_id
                INNER JOIN  `agent` ON order.store_id = agent.id
                WHERE order_detail.order_status_id = 2 and
                 MONTH( order_detail.order_created ) =12
                AND YEAR( order_detail.order_created ) =".$nam."
                GROUP BY agent.id, agent.name) AS T12 ON T.id = T12.id
                
                ");
        $rows = $stmt->fetchAll(PDO::FETCH_CLASS);
        $stmt->closeCursor();
        $entries   = array();
        foreach ($rows as $row) {
            //$entry = new Model_Province();
            array_push($entries, $row);
            //$entries[] = $entry;
        }
        return $entries;
    }
}

